package mvp.com.register.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.text.TextUtils;

/**
 * Created by YoungerChen on 2016/9/14.
 */
public class SPManager {
    //存储的sharedpreferences文件名
    private static final String DEFAULT_PREFS = "shared_prefs";
    public static final String USER_PREFS = "user_prefs";

    /**
     * 保存数据到文件
     *
     * @param context
     * @param key
     * @param data
     */
    public static void saveData(Context context, String key, Object data) {
        saveData(context, null, key, data);
    }

    /**
     * 保存数据到指定文件
     *
     * @param context
     * @param key
     * @param data
     */
    public static void saveData(Context context, String fileName, String key, Object data) {
        if (data == null)
            return;
        String type = data.getClass().getSimpleName();
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(getFileName(fileName), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        if ("Integer".equals(type)) {
            editor.putInt(key, (Integer) data);
        } else if ("Boolean".equals(type)) {
            editor.putBoolean(key, (Boolean) data);
        } else if ("String".equals(type)) {
            editor.putString(key, (String) data);
        } else if ("Float".equals(type)) {
            editor.putFloat(key, (Float) data);
        } else if ("Long".equals(type)) {
            editor.putLong(key, (Long) data);
        }
        editor.commit();
    }

    @NonNull
    private static String getFileName(String fileName) {
        return TextUtils.isEmpty(fileName) ? DEFAULT_PREFS : fileName;
    }

    /**
     * 从默认文件中读取数据
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static Object getData(Context context, String key, Object defValue) {
        return getData(context, null, key, defValue);
    }

    /**
     * 从指定文件中读取数据
     *
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static Object getData(Context context, String fileName, String key, Object defValue) {
        if (defValue == null)
            return null;
        String type = defValue.getClass().getSimpleName();
        SharedPreferences sharedPreferences = context.getSharedPreferences
                (getFileName(fileName), Context.MODE_PRIVATE);

        //defValue为为默认值，如果当前获取不到数据就返回它
        if ("Integer".equals(type)) {
            return sharedPreferences.getInt(key, (Integer) defValue);
        } else if ("Boolean".equals(type)) {
            return sharedPreferences.getBoolean(key, (Boolean) defValue);
        } else if ("String".equals(type)) {
            return sharedPreferences.getString(key, "");
        } else if ("Float".equals(type)) {
            return sharedPreferences.getFloat(key, (Float) defValue);
        } else if ("Long".equals(type)) {
            return sharedPreferences.getLong(key, (Long) defValue);
        }

        return null;
    }

    /**
     * 移除指定的SharedPreference文件
     *
     * @param context
     */
    public static void remove(Context context, String fileName) {
        SharedPreferences sharedPreferences = context.getSharedPreferences
                (getFileName(fileName), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.clear();
        editor.commit();
    }


}
